package cn.tao.base.service.impl;

import cn.tao.base.mapper.BaseMapper;
import cn.tao.base.query.BaseQuery;
import cn.tao.base.service.IBaseService;
import cn.tao.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }


    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }


    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    /**
     * 高级查询和分页
     * @param query
     * @return
     */
    @Override
    public QueryResult<T> findByQuery(BaseQuery query) {
        //数据总条数
        Long count = baseMapper.queryCount(query);
        //每页要展示的数据
        List<T> data = baseMapper.queryData(query);
        return new QueryResult<T>(count,data);
    }


}
